package cn.songhaiqing.walle.synccontroller;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.graphics.drawable.PathInterpolatorCompat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import cn.songhaiqing.walle.bean.HistoryHour;
import cn.songhaiqing.walle.bean.MyApp;
import cn.songhaiqing.walle.bean.NewSleepHis;
import cn.songhaiqing.walle.ble_controller.BleConnectController;
import cn.songhaiqing.walle.ble_controller.NoConnectException;
import cn.songhaiqing.walle.help.CommandHelper;
import cn.songhaiqing.walle.help.DeviceHelper;
import cn.songhaiqing.walle.help.FormatHelper;
import cn.songhaiqing.walle.help.SpHelper;

public abstract class SyncCompat {
    public static final int SYNC_TYPE_ACTIVITY = 10;
    public static final int SYNC_TYPE_ACTIVITY_COUNT = 9;
    public static final int SYNC_TYPE_ALARM = 4;
    public static final int SYNC_TYPE_ALARM2 = 13;
    public static final int SYNC_TYPE_BATTERY = 12;
    public static final int SYNC_TYPE_BLOOD = 16;
    public static final int SYNC_TYPE_BRACELET_CLEAN = 15;
    public static final int SYNC_TYPE_FINISH = 17;
    public static final int SYNC_TYPE_HEART = 6;
    public static final int SYNC_TYPE_PROFILE = 3;
    public static final int SYNC_TYPE_RESET = 14;
    public static final int SYNC_TYPE_SLEEP = 11;
    public static final int SYNC_TYPE_TARGET = 5;
    public static final int SYNC_TYPE_TIME = 7;
    public static final int SYNC_TYPE_TIME_FORMAT = 8;
    public static final int SYNC_TYPE_TOUCHVIBRATE = 2;
    public static final int SYNC_TYPE_UNIT = 1;
    public static final int SYNC_TYPE_VERSION = 0;
    private final int TIMEOUT =1;// PathInterpolatorCompat.MAX_NUM_POINTS;
    protected Context context;
    protected BleConnectController controller;
    protected int count = 0;
    protected int currentSn = 0;
    protected Handler handler = new Handler(Looper.getMainLooper());
    protected int hour = 0;
    protected int hourLabel = 18;
    protected int index = 1;
    protected boolean isFirstConnect = true;
    protected boolean isSyncing = false;
    protected ArrayList<NewSleepHis> newSleepHises = new ArrayList<>();
    protected onSyncStateChangeListener onSyncStateChangeListener;
    protected int profileID;
    protected List<HistoryHour> sleepHourList = new ArrayList();
    protected int sn = 0;
    protected List<HistoryHour> stepHourList = new ArrayList();
    protected Runnable timeOutrunnable;
    protected int timer_sync_process_timeout_count = 0;

    public interface BLEDataCallBack {
        void getSyncData(byte[] bArr) throws NoConnectException;
    }

    public interface onSyncStateChangeListener {
        void onBatteryReceived();

        void onSingleSyncFail();

        void onSyncFail();

        void onSyncFinish();

        void onSyncOver();
    }

    public abstract void getSyncData(byte[] bArr) throws NoConnectException;

    public abstract void setCallRemaind(String str) throws NoConnectException;

    public abstract void setSmsRemaind(String str, String str2) throws NoConnectException;

    public abstract void startSync() throws NoConnectException;

    /* access modifiers changed from: protected */
    public abstract void sync(int i) throws NoConnectException;

    public void startSyncProcessTimer(final int type) {
        if (this.timeOutrunnable != null) {
            this.handler.removeCallbacks(this.timeOutrunnable);
        }
        this.timeOutrunnable = new Runnable() {
            /* class com.gzgamut.smart_movement.synccontroller.SyncCompat.AnonymousClass1 */

            public void run() {
                if (SyncCompat.this.timer_sync_process_timeout_count == 0) {
                    try {
                        SyncCompat.this.sync(type);
                    } catch (NoConnectException e) {
                        e.printStackTrace();
                    }
                    SyncCompat.this.timer_sync_process_timeout_count++;
                    return;
                }
                SyncCompat.this.handler.removeCallbacks(SyncCompat.this.timeOutrunnable);
                SyncCompat.this.timer_sync_process_timeout_count = 0;
                if (SyncCompat.this.onSyncStateChangeListener == null) {
                    return;
                }
                if (SyncCompat.this.isSyncing) {
                    SyncCompat.this.isSyncing = false;
                    SyncCompat.this.onSyncStateChangeListener.onSyncFail();
                    return;
                }
                SyncCompat.this.onSyncStateChangeListener.onSingleSyncFail();
            }
        };
        if (this.isSyncing) {
            this.handler.postDelayed(this.timeOutrunnable, 3000);
        }
    }

    public void stopSyncProcessTimer() {
        if (this.timeOutrunnable != null) {
            this.handler.removeCallbacks(this.timeOutrunnable);
        }
    }

    public void setIsFirstConnect(boolean isFirstConnect2) {
        this.isFirstConnect = isFirstConnect2;
    }

    public void setIsSyncing(boolean isSyncing2) {
        this.isSyncing = isSyncing2;
    }

    public boolean isSyncing() {
        return this.isSyncing;
    }

    public static SyncCompat getSyncCompatImp(int type, BleConnectController controller2) {
        switch (type) {
            case 1:
                return new SyncCompatImpSH06(controller2);
            case 2:
            case 4:
            case 6:
            case 7:
            case 14:
            default:
                return null;
            case 3:
                if (DeviceHelper.isSH09NewSleep()) {
                    return new SyncCompatImpSH09NewSleep(controller2);
                }
                return new SyncCompatImpSH09(controller2);
            case 5:
                if (DeviceHelper.isSh09uColorScreen()) {
                    if (DeviceHelper.isSh09uColorScreenNewSleep()) {
                        return new SyncCompatImpSH09UColorfulNewSleep(controller2);
                    }
                    return new SyncCompatImpSH09UColorful(controller2);
                } else if (DeviceHelper.isSh09uUnColorScreenNewSleep()) {
                  //  return new SyncCompatImpSH09UUncolorfulNewSleep(controller2);
                } else {
                    return new SyncCompatImpSH09UUncolorful(controller2);
                }
            case 8:
                if (DeviceHelper.isI7PColorScreenNewSleep()) {
                 //   return new SyncCompatImpI7PLUSNewSleep(controller2);
                }
             //   return new SyncCompatImpI7PLUS(controller2);
            case 9:
                if (DeviceHelper.isI10NewSleep()) {
                  //  return new SyncCompatImpI10NewSleep(controller2);
                }
             //   return new SyncCompatImpI10(controller2);
            case 10:
             //   return new SyncCompatImpI3(controller2);

            case 16:

                return null;
            case 17:

                return null;
        }
    }

    public static int getProfileID(Context context2) {

        return 1;
    }

    public void setOnSyncStateChangeListener(onSyncStateChangeListener onSyncStateChangeListener2) {
        this.onSyncStateChangeListener = onSyncStateChangeListener2;
    }

    /* access modifiers changed from: protected */
    public void saveData() {
        try {
            this.stepHourList =null;// CalculateHelper.calculateAllBurn(this.context, this.stepHourList);
            this.profileID = getProfileID(this.context);
           // DatabaseProvider.getInstance(this.context).saveHistoryHour(this.context, this.stepHourList, this.profileID, 5);
          //  DatabaseProvider.getInstance(this.context).saveHistoryHour(this.context, this.sleepHourList, this.profileID, 2);
            dealDailyData();
            isSaveFirstSyncTime();
            this.isFirstConnect = false;
            this.isSyncing = false;
        } catch (Exception e) {
            this.isSyncing = false;
            if (this.onSyncStateChangeListener != null) {
                this.onSyncStateChangeListener.onSyncFail();
            }
        }
    }

    /* access modifiers changed from: protected */
    public void dealDailyData() {
        ArrayList<Calendar> saveCals = new ArrayList<>();
        if (this.stepHourList != null && this.stepHourList.size() > 0) {
            int type = DeviceHelper.getDefaultDevice();
            for (HistoryHour his : this.stepHourList) {
                Calendar his_cal =null; //CalendarHelper.setDayFormat(his.getDate());
                if (!saveCals.contains(his_cal)) {
                    saveCals.add(his_cal);
                }
            }
            Iterator<Calendar> it = saveCals.iterator();
            while (it.hasNext()) {
              //  DealOldDataHelper.getDataByDateAndSaveNew(this.context, type, this.profileID, it.next());
            }
        }
    }

    /* access modifiers changed from: protected */
    public void saveNewSleepData() {
        try {
            this.profileID = getProfileID(this.context);
         //   DatabaseProvider.getInstance(this.context).saveNewSleepHour(this.context, this.newSleepHises, this.profileID);
            isSaveFirstSyncTime();
            this.isFirstConnect = false;
            this.isSyncing = false;
        } catch (Exception e) {
            this.isSyncing = false;
            if (this.onSyncStateChangeListener != null) {
                this.onSyncStateChangeListener.onSyncFail();
            }
        }
    }

    public void setProfile() throws NoConnectException {
        writeData(CommandHelper.getProfileValue(this.context));
    }

    public void setAlarm() throws NoConnectException {
        writeData(CommandHelper.getAlarmValue(this.context));
    }

    public void setTarget() throws NoConnectException {
        writeData(CommandHelper.getGoalValue(this.context));
    }

    public void setTime() throws NoConnectException {
        writeData(CommandHelper.getDateTimeValue());
    }

    /**
     * 获取历史计步数据 {"sn":5,"count":7,"year":2021,"month":1,"day":1}
     * @throws NoConnectException
     */
    public void getActivityCount() throws NoConnectException {
        writeData(CommandHelper.getActivityCountValue());
    }

    public void getActivity(int sn2, int hour2) throws NoConnectException {
        writeData(CommandHelper.getActivityValue(sn2, hour2));
    }

    public void getSleep(int sn2, int hour2) throws NoConnectException {
        writeData(CommandHelper.getSleepValue(sn2, hour2));
    }

    public void getNewSleep(int sn2, int hour2) throws NoConnectException {
        writeData(CommandHelper.getSleepValueNew(sn2, hour2));
    }

    public void getBattery() throws NoConnectException {
        writeData(CommandHelper.getBatteryValue());
    }

    public void setFinishSync() throws NoConnectException {
        byte[] value = new byte[20];
        value[0] = 113;
        writeData(value);
    }

    public void writeData(byte[] value) throws NoConnectException {
     //   this.controller.self_writeData(Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
    }

    public void allRemaind(String content, int type) throws NoConnectException {
        if (!MyApp.getIntance().isSending()) {
            MyApp.getIntance().setIsSending(true);
            sendNotifi(content, type);
            this.handler.postDelayed(new Runnable() {
                /* class com.gzgamut.smart_movement.synccontroller.SyncCompat.AnonymousClass2 */

                public void run() {
                    MyApp.getIntance().setIsSending(false);
                }
            }, 3000);
        }
    }

    public void sendNotifi(String content, int type) throws NoConnectException {
        String send;
        int count2 = getPackageCount(content);
        int id = SpHelper.getInt(SpHelper.NOTIFICATION_SEND_ID, 1);
        for (int i = 0; i < count2; i++) {
            if (i != count2 - 1) {
                send = content.substring(i * 8, (i * 8) + 8);
            } else if (count2 * 8 > content.length()) {
                send = content.substring(i * 8, content.length());
            } else {
                send = content.substring(i * 8, count2 * 8);
            }
            writeData(getBleSendValue(count2, type, id, send.toCharArray()));
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (count2 == 0) {
            byte[] value = new byte[20];
            value[0] = 32;
            value[1] = (byte) id;
            value[2] = 1;
            value[3] = (byte) type;
            writeData(value);
        }
        if (id == 255) {
            SpHelper.putInt(SpHelper.NOTIFICATION_SEND_ID, 1);
        } else {
            SpHelper.putInt(SpHelper.NOTIFICATION_SEND_ID, id + 1);
        }
    }

    public int getPackageCount(String content) {
        int count2;
        if (content.length() > 48) {
            count2 = 6;
            String content2 = content.substring(0, 47) + "…";
        } else if (content.length() == 48) {
            count2 = 6;
        } else if (content == null || content.length() == 0) {
            return 0;
        } else {
            int merchant = content.length() / 8;
            if (content.length() % 8 == 0) {
                count2 = merchant;
            } else {
                count2 = merchant + 1;
            }
        }
        return count2;
    }

    private byte[] getBleSendValue(int count2, int type, int id, char[] content) {
        byte[] value = new byte[20];
        value[0] = 32;
        value[1] = (byte) id;
        value[2] = (byte) count2;
        value[3] = (byte) type;
        for (int i = 0; i < content.length; i++) {
            byte[] b = charToByte(content[i]);
            value[(i * 2) + 4] = b[0];
            value[(i * 2) + 5] = b[1];
        }
        if (content.length < 8) {
            for (int i2 = (content.length * 2) + 4; i2 < value.length; i2++) {
                value[i2] = 0;
            }
        }
        return value;
    }

    public byte[] charToByte(char c) {
        return new byte[]{(byte) ((65280 & c) >> 8), (byte) (c & 255)};
    }

    /* access modifiers changed from: protected */
    public void isSaveFirstSyncTime() {
        Calendar calendar = Calendar.getInstance();
        String nowDate = FormatHelper.sdf_yyyyMMdd.format(calendar.getTime());
        String hour_minute = FormatHelper.sdf_HHmm.format(calendar.getTime());

    }
}
